package com.qf.controller;

import com.qf.entity.Good;
import com.qf.entity.Page;
import com.qf.service.GoodService;
import com.qf.service.impl.GoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/17 21:13
 * @Version 1.0
 */
@WebServlet(name = "GoodController",value = "/good")
public class GoodController extends BaseController {
    private GoodService service=new GoodServiceImpl();
    public  String getGoodsListByTypeId(HttpServletRequest request,HttpServletResponse response){
        String strTypeId=request.getParameter("typeId");
        String strPageIndex=request.getParameter("pageIndex");
        Integer typeId=0;
        //做这个判断的原因是我们在登录成功后来到该页面没有传递typeId值过来，所以默认查询手机下面的分类
        if (strTypeId==null){
            //默认查询手机下面的商品
            typeId=1;
        }else {
            typeId=Integer.parseInt(strTypeId);
        }
        int pageIndex=0;
        if (strPageIndex==null){
            pageIndex=1;
        }else {
            pageIndex=Integer.parseInt(strPageIndex);
        }
        Page page=new Page(pageIndex);
        List<Good> list=service.getGoodsListByTypeId(typeId,page);
        System.out.println(list.toString());
        request.setAttribute("goods",list);//展示数据的
        request.setAttribute("page",page);//做分页的
        request.setAttribute("typeId",typeId);//传过去是为了下次当用户点击分页按钮时i，必须把值重新传递过来
        return "forward:index.jsp";

    }
    //展示订单详情
    public String showGoodDetail(HttpServletRequest request,HttpServletResponse response){
        String strGoodId =request.getParameter("goodId");
        Good good=service.showGoodDetail(Integer.parseInt(strGoodId));
        request.setAttribute("good",good);

        return "forward:goodsDetail.jsp";
    }





}
